package com.ruoyi.system.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.TbChargingpilesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbChargingpilesstationMapper;
import com.ruoyi.system.domain.TbChargingpilesstation;
import com.ruoyi.system.service.ITbChargingpilesstationService;

/**
 * 充电站Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-12
 */
@Service
public class TbChargingpilesstationServiceImpl implements ITbChargingpilesstationService 
{
    @Autowired
    private TbChargingpilesstationMapper tbChargingpilesstationMapper;
    @Autowired
    private TbChargingpilesMapper tbChargingpilesMapper;

    /**
     * 查询充电站
     * 
     * @param sid 充电站主键
     * @return 充电站
     */
    @Override
    public TbChargingpilesstation selectTbChargingpilesstationBySid(Long sid)
    {
        return tbChargingpilesstationMapper.selectTbChargingpilesstationBySid(sid);
    }

    /**
     * 查询充电站列表
     * 
     * @param tbChargingpilesstation 充电站
     * @return 充电站
     */
    @Override
    public List<TbChargingpilesstation> selectTbChargingpilesstationList(TbChargingpilesstation tbChargingpilesstation) {
        List<TbChargingpilesstation> tbChargingpilesstations = tbChargingpilesstationMapper.selectTbChargingpilesstationList(tbChargingpilesstation);
        Long uid = SecurityUtils.getLoginUser().getUserId();
        List<Long> sids = tbChargingpilesstationMapper.getsidByUid(uid);

        // 设置充电桩数量和正在使用数量
        tbChargingpilesstations.forEach(tbChargingpilesstation1 -> {
            tbChargingpilesstation1.setUse(tbChargingpilesMapper.getAllUsingChargingPilesNumBySid(tbChargingpilesstation1.getSid()));
            tbChargingpilesstation1.setNumber(tbChargingpilesMapper.getAllChargingPilesNumBySid(tbChargingpilesstation1.getSid()));
        });

        // 将非权限所属的充电站去除
        if (!SecurityUtils.isAdmin(uid)) {
            List<TbChargingpilesstation> list = tbChargingpilesstations.stream().filter(station -> sids.contains(station.getSid().intValue())).collect(Collectors.toList());
            return list;
        }

        return tbChargingpilesstations;
    }


    /**
     * 新增充电站
     * 
     * @param tbChargingpilesstation 充电站
     * @return 结果
     */
    @Override
    public int insertTbChargingpilesstation(TbChargingpilesstation tbChargingpilesstation)
    {
        int i = tbChargingpilesstationMapper.insertTbChargingpilesstation(tbChargingpilesstation);
        Long userId = SecurityUtils.getLoginUser().getUserId();
        tbChargingpilesstationMapper.userstation(userId,tbChargingpilesstation.getSid());
        return i;
    }

    /**
     * 修改充电站
     * 
     * @param tbChargingpilesstation 充电站
     * @return 结果
     */
    @Override
    public int updateTbChargingpilesstation(TbChargingpilesstation tbChargingpilesstation)
    {

        return tbChargingpilesstationMapper.updateTbChargingpilesstation(tbChargingpilesstation);
    }

    /**
     * 批量删除充电站
     * 
     * @param sids 需要删除的充电站主键
     * @return 结果
     */
    @Override
    public int deleteTbChargingpilesstationBySids(Long[] sids)
    {
        return tbChargingpilesstationMapper.deleteTbChargingpilesstationBySids(sids);
    }

    /**
     * 删除充电站信息
     * 
     * @param sid 充电站主键
     * @return 结果
     */
    @Override
    public int deleteTbChargingpilesstationBySid(Long sid)
    {
        tbChargingpilesstationMapper.deluserstation(sid);
        return tbChargingpilesstationMapper.deleteTbChargingpilesstationBySid(sid);
    }

    @Override
    public List<TbChargingpilesstation> selectAllChargingpilesstation() {
        List<TbChargingpilesstation> tbChargingpilesstations = tbChargingpilesstationMapper.selectAllChargingpilesstation();        tbChargingpilesstations.forEach(tbChargingpilesstation1 -> {
            tbChargingpilesstation1.setUse(tbChargingpilesMapper.getAllUsingChargingPilesNumBySid(tbChargingpilesstation1.getSid()));
            tbChargingpilesstation1.setNumber(tbChargingpilesMapper.getAllChargingPilesNumBySid(tbChargingpilesstation1.getSid()));
        });

        return tbChargingpilesstations;
    }
}
